package com.ydd.app.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ydd.app.action.CommonAct;
import com.ydd.app.bean.common.CompanyInfoBean;
import com.ydd.app.bean.common.CompanyTypeBean;
import com.ydd.app.bean.common.ConfigTypeBean;
import com.ydd.app.bean.input.CreateCompanyRequest;
import com.ydd.app.bean.input.CreateConfigTypesRequest;
import com.ydd.app.bean.input.UpdateDisplayFieldRequest;
import com.ydd.app.dao.AccountDao;
import com.ydd.app.dao.CompanyDao;
import com.ydd.app.manager.CompanyMng;
import com.ydd.common.util.CommonUtil;

@Service
public class CompanyMngImpl extends CommonAct implements CompanyMng{
	
	@Autowired
	private CompanyDao _companyDao;
	
	@Autowired
	private AccountDao _accountDao;

	@Override
	public CompanyInfoBean queryCompanyInfo(final String ssId, final String userId) {
		final CompanyInfoBean company = _companyDao.queryCompanyInfo(ssId,_accountDao.isInDispaly(userId));
		if( null != company ) {
			return company;
		}
		return null;
	}

	@Override
	public CompanyInfoBean[] queryCompanyInfos(final String companyTypeId, final int page,
			final int pageCount) {
		String typeId = null;
		if ( _companyDao.queryConfigTypeIsExist(companyTypeId)) {
			typeId = companyTypeId;
		}
		List<CompanyInfoBean> companies = _companyDao.queryCompanyInfos(typeId, page, pageCount);
		if( null != companies && companies.size() > 0 ) {
			return companies.toArray(new CompanyInfoBean[0]);
		}
		return null;
	}

	@Override
	public ConfigTypeBean[] queryNewestCompanyInfos(final int pageCount) {
		List<ConfigTypeBean> types = _companyDao.queryNewestCompanyInfos(pageCount);
		if( null != types && types.size() > 0 ) {
			return types.toArray(new ConfigTypeBean[0]);
		}
		return null;
	}

	@Override
	public ConfigTypeBean[] queryConfigTypes() {
		final List<ConfigTypeBean> types = _companyDao.queryConfigTypes();
		if ( null != types && types.size() > 0 ) {
			return types.toArray(new ConfigTypeBean[0]);
		}
		return null;
	}

	@Override
	public int queryCompanyCount(final String companyTypeId) {
		String typeId = null;
		if ( _companyDao.queryConfigTypeIsExist(companyTypeId)) {
			typeId = companyTypeId;
		}
		return _companyDao.queryCompanyCount(typeId);
	}

	@Override
	public CompanyTypeBean[] queryCompanyTypes() {
		final List<CompanyTypeBean> types = _companyDao.queryCompanyTypes();
		if ( null != types && types.size() > 0 ) {
			return types.toArray(new CompanyTypeBean[0]);
		}
		return null;
	}

	public boolean createCompany(final CreateCompanyRequest request){
		if( CommonUtil.isNotEmpty(request.getSysID())) {
			return _companyDao.createCompany(request);
		}
		return false;
	}

	@Override
	public boolean createCOnfigType(CreateConfigTypesRequest request) {
		if( CommonUtil.isNotEmpty(request.getSysID())) {
			return _companyDao.createCOnfigType(request);
		}
		return false;
	}
	
	public boolean updateDisplayField(final UpdateDisplayFieldRequest request){
		if( CommonUtil.isNotEmpty(request.getFieldName())) {
			return _companyDao.updateDisplayField(request);
		}
		return false;
	}

}
